package com.haust.search.controller;

import com.haust.commons.pojo.HaustResult;
import com.haust.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
public class SearchController {
    @Autowired
    private SearchService searchService;

    @PostMapping("/search/init")
    public HaustResult init() {
        return searchService.init();
    }

    @PostMapping("/search/list")
    public HaustResult list(String q,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "25") Integer rows){
        return searchService.list(q, page, rows);
    }

    /**
     * 根据 商品id ，删除Es中对应数据
     * @param itemId  商品id
     * @return
     */
    @PostMapping("/search/removeById")
    public HaustResult removeById(Long itemId){
        return searchService.removeById(itemId);
    }

    /**
     *  添加当个商品 到 ES中
     * @param item  某个商品
     * @return
     */
    @PostMapping("/search/add")
    public HaustResult add(@RequestBody Map<String,Object> item){
        return searchService.add(item);
    }
}
